<?php

// +----------------------------------------------------------------------
// | GuoRan100 [ 果然100 你的选择 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州鲜生活电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.guoran100.com>520
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Controller;
use Think\Model;
/**
 * 果然助理接口控制器
 * @author 曹梦龙 <138888611@qq.com>
 */
class AssistantController extends Controller {
   
    //获取仓库清单
    public function getWarehouseData(){
        $warehouselist=M('warehouse')->select();
        $tags = array('code' => 0,'data'=>$warehouselist);
        $this->ajaxReturn($tags); 
    }
    
    //获取仓库支持区域清单
     public function getAreaData($wid){
         $map['warehouse']=$wid;
        $Arealist=M('area')->where($map)->select();
        $tags = array('code' => 0,'data'=>$Arealist);
        $this->ajaxReturn($tags); 
    }
    
    //获取运单数据
    public function getYundanData(){
        $Model = new Model();
        $rq_q = I('rq_q');      //创建时间_前
        $rq_h = I('rq_h');      //创建时间_后
        $area_in= I('area_in'); 
        $str = "";
        
        if($rq_q != null){
            $str .= " and a.payTime >= ".$rq_q;
        }
        
        if($rq_h != null){
            $str .= " and a.payTime <= ".$rq_h;
        }
        
        if($area_in!=null){
            $str .=" and areaId in (".$area_in.")";
        }
        
        /***
         * 普通订单数据收集
         */
       
        // $sql = "select * from order a where  a.status >= 2000 and a.status <= 3000 ".$str." order by a.payTime desc";
        $sql = "select serialNo,orderid,areaId,areaName,payway,toName,toProvince,toCity,toArea,toStreet,toPhone,payTime,finalAmount,remark,status,isPrint from gr_vyundan a where a.status = 2000 ".$str;
      
        $list = $Model->query($sql);
      
        for($i=0;$i<count($list);$i++){
            $sql="select pname,specname,cartnum from gr_vorder_goods where orderid='".$list[$i]['orderid']."'";
            $goodslist = $Model->query($sql);
            $goodsstr='';
            for($n=0;$n<count($goodslist);$n++){
                $goodsstr=$goodsstr. $goodslist[$n]['pname']."(".$goodslist[$n]['specname'].")-(".$goodslist[$n]['cartnum']."件)/  ";
            } 
            $list[$i]['goods']=$goodsstr;
             $list[$i]['ordertype']=0;
        }
        
        
       /***
         * 团购订单数据收集
       */
        $sql = "select serialNo,orderid,areaId,areaName,payway,toName,toProvince,toCity,toArea,toStreet,toPhone,payTime,finalAmount,remark,status,isPrint from gr_vgp_order a where a.status = 2000 ".$str;
         $listgroup = $Model->query($sql);
           for($i=0;$i<count($listgroup);$i++){
            $sql="select pname,cartnum from gr_vgp_order_goods where orderid='".$listgroup[$i]['orderid']."'";
            $goodslist = $Model->query($sql);
            $goodsstr='【团购】';
            for($n=0;$n<count($goodslist);$n++){
                $goodsstr=$goodsstr. $goodslist[$n]['pname']."-(".$goodslist[$n]['cartnum']."件)/  ";
            }
           
            $listgroup[$i]['goods']=$goodsstr;
            $listgroup[$i]['ordertype']=1;
        }
        // dump($listgroup);
        
        $listAll =array_merge($list, $listgroup);
       // dump($listAll);
        if($listAll){
            $tags = array('code' => 0,'data'=>array('total' => count($listAll),'rows' =>$listAll ));
           $this->ajaxReturn($tags); 
        }else{
             $tags = array('code' => -1);
            $this->ajaxReturn($tags); 
        }
    } 

    //批量设置打印标记改变订单状态
    public function setYundanData($orderidarray){ 
          $map['orderid']  = array('in',$orderidarray);
          $map['status']=2000;
          $data['status'] = 3000;
          $data['syncStatus'] = 2;
          $data['isPrint'] = 1;
          $order = M("order"); 
          $result= $order->where($map)->save($data); 
          
          $gr_gp_order=M('gp_order');
           $result2= $gr_gp_order->where($map)->save($data); 
          if($result+$result2>0){
              //成功
               $tags = array('code' => $result+$result2);
               $this->ajaxReturn($tags); 
          }else{
              $tags = array('code' => -1);
              $this->ajaxReturn($tags); 
          }  
    }
    
    //获取
    public function test(){
        $imglist=M('product_image')->Field('fileurl')->select();
        echo json_encode($imglist);
    }
}
